package chapter1.view;

import chapter2.vo.DeptVo;
import chapter2.vo.BaseVo;
import chapter2.vo.Result;
import service.IBaseService;
import service.IDeptService;

public abstract class BaseView<Vo extends BaseVo,IService extends IBaseService> {

    IService service;
    Vo[] cache;
    void printError(String message) {
        System.out.println("\033[31m" + message + "\033[0m");
    }

    Vo getCacheById() {
        System.out.println("请输入部门ID,ID必须是大于0的整数");
        if (!MainView.input.hasNextInt()) {
            MainView.input.nextLine();
            printError("ID必须为整数，请重新输入!");
            return null;
        }
        int id = MainView.input.nextInt();
        MainView.input.nextLine();
        if (id <= 0) {
            printError("ID必须大于0，请重新输入!");
            return null;
        }
        if (cache == null) {
            Result result = service.list();
            cache = (Vo[]) result.data;
        }
        Vo vo = null;
        for (Vo item : cache) {
            if (item.getId() == id) {
                System.out.println("该ID对应的数据是");
                System.out.println(item);
                vo = item;
                break;
            }
        }
        if (vo == null) {
            printError("没有找到对应的数据，请重新输入!");
            return null;
        }
        return vo;
    }
}